package com.sky.schedule;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.*;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.UserMapper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

@Component
@Slf4j
@RequiredArgsConstructor
public class ReportTask {
    private final OrderMapper orderMapper;
    private final ReportMapper reportMapper;
    private final UserMapper userMapper;

    /**
     * 营业额数据统计
     */
    //@Scheduled(cron = "0/10 * * * * ?")
//    @Scheduled(cron = "0 0 2 * * ?")
//    @Scheduled(cron = "0/10 * * * * ?")
    @XxlJob("turnoverStatisticsTask")
    public void turnoverStatisticsTask() {
        log.info("营业额数据统计");
        //删除表的全部数据
        reportMapper.deleteTurnoverReport();
        //封装数据
        ArrayList<TurnoverReport> turnoverReports = new ArrayList<>();
        //获取日期列表
        ArrayList<LocalDate> dateList = new ArrayList<>();
        //营业额集合
        ArrayList<Double> turnoverList = new ArrayList<>();
        //获取当前日期的前一天
        LocalDate end = LocalDate.now().minusDays(1);
        //获取下单时间时间最早并且状态为已完成的数据
        List<Orders> ordersList = orderMapper.getAllOrders(null, null);
        //获取下单时间最早的订单
        LocalDate begin = ordersList.stream().min(Comparator.comparing(Orders::getOrderTime)).get().getOrderTime().toLocalDate();
        //获取日期列表
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        for (LocalDate date : dateList) {
            //获取0点0分0秒
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            //获取23点59分59秒
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            //封装查询条件
            HashMap map = new HashMap();
            map.put("beginTime", beginTime);
            map.put("endTime", endTime);
            map.put("status", Orders.COMPLETED);
            //查询数据库获取营业额
            Double turnover = reportMapper.turnoverStatistics(map);
            if (turnover == null) {
                turnover = 0.0;
            }
            //添加至营业额集合中
            turnoverList.add(turnover);
            TurnoverReport turnoverReport = new TurnoverReport(null, date, turnover);
            turnoverReports.add(turnoverReport);
        }
        //插入数据
        reportMapper.insertTurnoverReport(turnoverReports);
    }

    //        @Scheduled(cron = "0/10 * * * * ?")
    @XxlJob("userStatisticsTask")
    public void userStatisticsTask() {
        //删除表的全部数据
        reportMapper.deleteUserReport();
        //封装插入数据
        ArrayList<UserReport> userReports = new ArrayList<>();
        //获取日期列表
        ArrayList<LocalDate> dateList = new ArrayList<>();
        //获取当前日期的前一天
        LocalDate end = LocalDate.now().minusDays(1);
        //获取所有用户
        List<User> list = userMapper.getAllUsers();
        //获取最小的日期
        LocalDate begin = list.stream().min(Comparator.comparing(User::getCreateTime)).get().getCreateTime().toLocalDate();
        //获取日期列表
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        for (LocalDate date : dateList) {
            UserReport userReport = new UserReport();
            //获取0点0分0秒
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            //获取23点59分59秒
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            Integer usersCount = userMapper.getUsersByTime(beginTime, endTime);
            userReport.setId(null);
            userReport.setDate(date);
            userReport.setNumber(usersCount);

            userReports.add(userReport);
        }
        log.info("userReportLength--{}", userReports.size());
        //批量插入(一次插入1000条)
        for (int i = 0; i < userReports.size(); i += 1000) {
            int endIndex = Math.min(i + 1000, userReports.size());
            List<UserReport> subList = userReports.subList(i, endIndex);
            reportMapper.insertUserReport(subList);
        }
    }

    //    @Scheduled(cron = "0/10 * * * * ?")
    @XxlJob("orderStatisticsTask")
    public void orderStatisticsTask() {
        //删除表的全部数据
        reportMapper.deleteOrderReport();
        //封装返回数据
        ArrayList<OrderReport> orderReports = new ArrayList<>();
        //获取日期列表
        ArrayList<LocalDate> dateList = new ArrayList<>();
        //获取当前日期的前一天
        LocalDate end = LocalDate.now().minusDays(1);
        //获取下单时间时间最早并且状态为已完成的数据
        List<Orders> ordersList = orderMapper.getAllOrders(null, null);
        //获取下单时间最早的订单
        LocalDate begin = ordersList.stream().min(Comparator.comparing(Orders::getOrderTime)).get().getOrderTime().toLocalDate();
        //获取日期列表
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        for (LocalDate date : dateList) {
            //获取date的0:00:00
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            //获取date的23:59:59
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            //获取当日订单数量
            Integer orderCount = orderMapper.getOrderCount(beginTime, endTime, null);
            //获取当日有效订单数量
            Integer validOrderCount = orderMapper.getOrderCount(beginTime, endTime, Orders.COMPLETED);
            OrderReport orderReport = new OrderReport(null, date, orderCount, validOrderCount);
            orderReports.add(orderReport);
        }
        //插入数据
        reportMapper.insertOrderReport(orderReports);
    }

    //    @Scheduled(cron = "0/10 * * * * ?")
    @XxlJob("topTask")
    public void topTask() {
        //删除表的全部数据
        reportMapper.deleteTopReport();
        //封装返回数据
        ArrayList<TopReport> topReports = new ArrayList<>();
        //封装日期集合
        ArrayList<LocalDate> dateList = new ArrayList<>();
        //获取当前日期的前一天
        LocalDate end = LocalDate.now().minusDays(1);
        //获取下单时间最早的订单
        LocalDate begin = orderMapper.getAllOrders(null, null).stream().min(Comparator.comparing(Orders::getOrderTime)).get().getOrderTime().toLocalDate();
        //获取日期列表
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //获取菜品和套餐的名字和销量
        for (LocalDate date : dateList) {
            //获取date的0:00:00
            LocalDateTime beginTime = LocalDateTime.of(date, LocalTime.MIN);
            //获取date的23:59:59
            LocalDateTime endTime = LocalDateTime.of(date, LocalTime.MAX);
            //获取当日菜品/套餐销量
            List<GoodsSalesDTO> list = reportMapper.getSalesTop(beginTime, endTime);
            for (GoodsSalesDTO goodsSalesDTO : list) {
                TopReport topReport = new TopReport();
                topReport.setDate(date);
                topReport.setName(goodsSalesDTO.getName());
                topReport.setNumber(goodsSalesDTO.getNumber());
                topReports.add(topReport);
            }
        }
        //插入数据
        reportMapper.insertTopReport(topReports);
    }
}
